//
//  detailsConsultationViewController.m
//  DJS
//
//  Created by 浪尖渝力 on 2018/1/5.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "detailsConsultationViewController.h"
#import "detailsConsultationView.h"
#import "FenXiangView.h"
@interface detailsConsultationViewController ()
@property(nonatomic,strong)detailsConsultationView *aView;
@property(nonatomic,strong)UIView *fenXaingView;
@end

@implementation detailsConsultationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"资讯详情";
    _aView=[detailsConsultationView new];
    [self.view addSubview:_aView];
    [_aView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self loadDate];
    _aView.viewController=self;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"分享、收藏"] style:UIBarButtonItemStylePlain target:self action:@selector(creatFenXiangSouCahng)];
}
-(void)loadDate{
    
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    if (userDic!=nil) {
        NSNumber *waiter_id1=[userDic objectForKey:@"waiter_id"];
        NSString *waiter_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:_news_id,@"news_id",@"1",@"user_role",waiter_id,@"waiter_id", nil];
        [ZQTools AFNPostDataUrl:@"api/index/news_info" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            _aView.dic=responseObject;
            [_aView creatView];
        } anderrorBlock:nil];
    }
    else{
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:_news_id,@"news_id", nil];
        [ZQTools AFNPostDataUrl:@"api/index/news_info" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            _aView.dic=responseObject;
            [_aView creatView];
        } anderrorBlock:nil];
    }
  
}

-(void)creatFenXiangSouCahng{
    
    NSNumber *is_collect=[_aView.dic objectForKey:@"is_collect"];
    [_fenXaingView removeFromSuperview];
    _fenXaingView=[UIView new];
    [self.view addSubview:_fenXaingView];
    [_fenXaingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(NAV_HEIGHT);
        make.height.mas_equalTo(90);
        if (is_collect.integerValue==1) {
            make.width.mas_equalTo(130);
        }
        else{
             make.width.mas_equalTo(100);
        }
    }];
    _fenXaingView.layer.cornerRadius=5;
    _fenXaingView.backgroundColor=[UIColor whiteColor];
    
    
    
    
    UIView *view1=[UIView new];
    [_fenXaingView addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(45);
    }];
    view1.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(souChang:)];
    [view1 addGestureRecognizer:tapGesture];
    
    UIImageView *imageView=[UIImageView new];
    [view1 addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
        make.height.width.mas_equalTo(20);
    }];
   
    UILabel *label=[UILabel new];
    [view1 addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageView.mas_right).mas_equalTo(15);
        make.centerY.mas_equalTo(0);
    }];
    
    label.textColor=ColorWithRGB(49, 49, 49);
    label.font=[UIFont systemFontOfSize:17];
    
    if (is_collect.integerValue==0) {
        label.text=@"收藏";
        imageView.image=[UIImage imageNamed:@"收藏"];
    }
    else{
        label.text=@"取消收藏";
        imageView.image=[UIImage imageNamed:@"收藏1"];
    }
    
    UIView *view2=[UIView new];
    [_fenXaingView addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(45);
    }];
    
    view2.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fenXiang)];
    [view2 addGestureRecognizer:tapGesture2];
    
    UIImageView *imageView2=[UIImageView new];
    [view2 addSubview:imageView2];
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
        make.height.width.mas_equalTo(20);
    }];
   

    imageView2.image=[UIImage imageNamed:@"Share-"];
  
    UILabel *label2=[UILabel new];
    [view2 addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageView.mas_right).mas_equalTo(15);
        make.centerY.mas_equalTo(0);
    }];
    label2.text=@"分享";
    label2.textColor=ColorWithRGB(49, 49, 49);
    label2.font=[UIFont systemFontOfSize:17];
    
    
    
}
-(void)souChang:(UITapGestureRecognizer *)tap{
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    if (userDic==nil) {
        [ZQTools ToLoging:self];
        return;
    }
    NSString *token=[userDic objectForKey:@"token"];
    NSNumber *waiter_id1=[userDic objectForKey:@"waiter_id"];
    NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
     NSNumber *is_collect=[_aView.dic objectForKey:@"is_collect"];
    if (is_collect.integerValue==0) {
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",user_id,@"waiter_id",@"1",@"user_role",_news_id,@"collect_object_id", nil];
        [ZQTools AFNPostDataUrl:@"api/center/collect" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            
            NSMutableDictionary *typeDic=[NSMutableDictionary dictionaryWithDictionary:_aView.dic];
            [typeDic setObject:@"1" forKey:@"is_collect"];
            [typeDic setObject:responseObject forKey:@"collect_id"];
            _aView.dic=typeDic;
            [_fenXaingView removeFromSuperview];
            
        } anderrorBlock:nil ];
    }
    else{
        NSNumber *collect_id=[_aView.dic objectForKey:@"collect_id"];
          NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",user_id,@"waiter_id",user_id,@"collect_person_id",@"1",@"user_role",[NSString  stringWithFormat:@"%@",collect_id],@"collect_id", nil];
        [ZQTools AFNPostDataUrl:@"api/center/del_collect" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            NSMutableDictionary *typeDic=[NSMutableDictionary dictionaryWithDictionary:_aView.dic];
            [typeDic setObject:@"0" forKey:@"is_collect"];
            _aView.dic=typeDic;
            [_fenXaingView removeFromSuperview];
            
        } anderrorBlock:nil ];
    }
    
   
    
}

#pragma mark————————————————————点击屏幕关闭键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view ];
    
    if (!(touchPoint.y>NAV_HEIGHT&&touchPoint.y<NAV_HEIGHT+90&&touchPoint.x>_mainW-130)) {
        [_fenXaingView removeFromSuperview];
    }
    
    
}
-(void)fenXiang{
    [_fenXaingView removeFromSuperview];
    FenXiangView *fenxiang=[FenXiangView new];
    [self.view addSubview:fenxiang];
    [fenxiang mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    fenxiang.themID=_news_id;
    fenxiang.kLinkTitle=[_aView.dic objectForKey:@"news_title"];
    fenxiang.kLinkDescription=[_aView.dic objectForKey:@"news_content"];
    [fenxiang creatView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
